package com.tree.thread;

/**
 * @ClassName com.tree.thread
 * Description: <类功能描述>. <br>
 * <p>
 * <使用说明>
 * </p>
 * @Author tree
 * @Date 2019-10-26 23:42
 * @Version 1.0
 */
public class Test {

    public static void main(String[] args) {
        Test001 test = new Test001();
        Thread t1 = new Thread(test,"窗口1");
        Thread t2 = new Thread(test,"窗口2");
        t1.start();
        //test.flag=false;
        t2.start();
    }


}

class Test001 implements Runnable{

    private int count = 100;
    private Object object = new Object();
    public boolean flag = true;

    @Override
    public void run() {
        if(flag){
            while (count > 0) {
                synchronized (this) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    if (count > 0) {
                        System.out.println("flag=false,"+Thread.currentThread().getName()+",出售第"+(100-count+1)+"票");
                        count--;
                    }

                }
//                sale();
            }
        }else {
            while (count > 0){
                sale();
            }
        }

    }

    private synchronized void sale() {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (count > 0) {
            System.out.println("flag=true,"+Thread.currentThread().getName()+",出售第"+(100-count+1)+"票");
            count--;
        }

    }
}
